package com.chanjiedata.changbs.module.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Connect;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.ruffian.library.widget.RCheckBox;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 文件名 : PayPasswordChangeActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 16:04
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class PayPasswordCheckActivity extends BaseActivity {

    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.cb_one)
    RCheckBox cbOne;
    @BindView(R.id.cb_two)
    RCheckBox cbTwo;
    @BindView(R.id.cb_three)
    RCheckBox cbThree;
    @BindView(R.id.cb_four)
    RCheckBox cbFour;
    @BindView(R.id.cb_five)
    RCheckBox cbFive;
    @BindView(R.id.cb_six)
    RCheckBox cbSix;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.pay_password_title)
    TextView payPasswordTitle;
    @BindView(R.id.pay_password_message)
    TextView payPasswordMessage;
    private List<RCheckBox> checkBoxList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activit_pay_password);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        backTv.setOnClickListener(v -> finish());
        init();
    }

    private void init() {
        tvTitle.setText(getIntent().getStringExtra("activity_title"));
        payPasswordTitle.setText(getIntent().getStringExtra("title"));
        payPasswordMessage.setText(getIntent().getStringExtra("message"));

        checkBoxList.add(cbOne);
        checkBoxList.add(cbTwo);
        checkBoxList.add(cbThree);
        checkBoxList.add(cbFour);
        checkBoxList.add(cbFive);
        checkBoxList.add(cbSix);


        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                for (RCheckBox rCheckBox : checkBoxList) {
                    rCheckBox.setChecked(false);
                }
                for (int i = 0; i < s.length(); i++) {
                    checkBoxList.get(i).setChecked(true);
                }
                //输入完6个验证码 自动请求验证
                if (s.length() == 6) {
                    checkPassword(s.toString());
                }
            }
        });
        etInput.setFocusable(true);
        etInput.setFocusableInTouchMode(true);
        etInput.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    private void checkPassword(String password) {
        Connect.sms_register.Builder builder = Connect.sms_register.newBuilder();
        builder.setPayword(password);
        showProgress();
        NetworkManager.INSTANCE.post(Apis.check_pd_pwd2, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                if (errorCode == 200) {
                    startActivity(BindBankCardActivity.class);
                } else {
                    showToast(errorMessage);
                    etInput.setText("");
                }
            }
        });
    }


}
